package P150;

/**
 * @author LiangMu
 * @version \$Id: P18.java, v 0.1, 2024年07月04日 18:53 LiangMu Exp $$
 */
public class P18 {
    public static int lengthOfLastWord(String s) {
        char[] chars = s.toCharArray();
        boolean b = true;
        int res = 0;
        for (int i = chars.length - 1; i >= 0; --i) {
            if (b) {
                if (chars[i] != ' '){
                    res++;
                    b = false;
                }
            } else {
                if (chars[i] == ' ') {
                    return res;
                }
                res++;
            }

        }
        return res;
    }

    public static void main(String[] args) {
        String s = "hello world";
        System.out.println(lengthOfLastWord(s));
    }
}
